package bb.lanxing.activity;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.github.chrisbanes.photoview.PhotoView;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.common.config.Constants;
import bb.lanxing.util.CustomShareUtil;
import bb.lanxing.util.AppFileUtils;
import bb.lanxing.view.BiciAlertDialogBuilder;
import bb.lanxing.view.PhotoViewPager;

public class PhotoViewerActivity extends BaseActivity {
    private RelativeLayout mainLayout;
    private PhotoViewPager photoViewPager;
    private CustomTarget<Drawable> target;
    private CustomTarget<Drawable> target2;

    private int defaultDrawableRes;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_photo_viewer);
        mainLayout = findViewById(R.id.main_layout);
        photoViewPager = findViewById(R.id.photo_view_pager);

        defaultDrawableRes = getIntent().getIntExtra("default_img", R.drawable.bg_photo_default);
        String stringExtra = getIntent().getStringExtra("photo_url");
        if (stringExtra != null) {
            displaySingleImage(stringExtra);
            return;
        }
        final String[] stringArrayExtra = getIntent().getStringArrayExtra("photo_url_array");
        if (stringArrayExtra == null || stringArrayExtra.length == 0) {
            return;
        }
        photoViewPager.setDefaultDrawable(defaultDrawableRes);
        photoViewPager.init(this, stringArrayExtra, true);
        int intExtra = getIntent().getIntExtra("cur_index", 0);
        photoViewPager.setCurIndex(intExtra);
        photoViewPager.setOnItemClickListener(new PhotoViewPager.OnItemClickListener() {
            @Override
            public void onItemClick(int i) {
                ActivityCompat.finishAfterTransition(PhotoViewerActivity.this);
            }

            @Override
            public void onPhotoTap(int i) {
                ActivityCompat.finishAfterTransition(PhotoViewerActivity.this);
            }

            @Override
            public void onItemLongClick(int i) {
                confirmSaveImage(stringArrayExtra[i]);
            }
        });
    }

    private void displaySingleImage(final String str) {
        final PhotoView photoView = new PhotoView(this);
        photoView.setTransitionName("imageView");
        photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mainLayout.addView(photoView, -1, -1);
        photoView.setOnClickListener(view -> ActivityCompat.finishAfterTransition(PhotoViewerActivity.this));
        photoView.setOnLongClickListener(view -> {
            confirmSaveImage(str);
            return true;
        });
        target = new CustomTarget<>() {

            @Override
            public void onLoadStarted(Drawable drawable) {
                super.onLoadStarted(drawable);
                showLoadingPopWindow();
            }

            @Override
            public void onLoadFailed(Drawable drawable) {
                super.onLoadFailed(drawable);
                closePopWindow();
                photoView.setImageResource(R.drawable.ic_default);
            }

            public void onResourceReady(@NonNull Drawable drawable, Transition<? super Drawable> transition) {
                closePopWindow();
                photoView.setImageDrawable(drawable);
            }

            @Override
            public void onLoadCleared(Drawable drawable) {
                closePopWindow();
            }
        };
        Glide.with(this).load(str).into(target);
    }

    public void confirmSaveImage(final String str) {
        new BiciAlertDialogBuilder(this).setItems(new CharSequence[]{"保存到手机"}, (dialogInterface, i) -> {
            if (i != 0) {
                return;
            }
            saveImageToLocal(str);
        }).show();
    }

    public void saveImageToLocal(String str) {
        target2 = new CustomTarget<>() {
            @Override
            public void onLoadCleared(Drawable drawable) {
            }

            @Override
            public void onLoadStarted(Drawable drawable) {
                super.onLoadStarted(drawable);
            }

            @Override
            public void onLoadFailed(Drawable drawable) {
                super.onLoadFailed(drawable);
                runOnUiThread(() -> App.getContext().showMessage("图像保存失败"));
            }

            public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
                String buildExternalDirectoryPath = AppFileUtils.buildExternalDirectoryPath(Constants.IMAGES_DIR);
                if (buildExternalDirectoryPath == null) {
                    return;
                }
                String str2 = System.currentTimeMillis() + ".jpg";
                final String str3 = buildExternalDirectoryPath + str2;
                CustomShareUtil.saveToLocal(PhotoViewerActivity.this, ((BitmapDrawable) drawable).getBitmap(), buildExternalDirectoryPath, str2);
                runOnUiThread(() -> Toast.makeText(PhotoViewerActivity.this, String.format("图片已保存至 \"%s\"", str3), Toast.LENGTH_LONG).show());
            }
        };
        Glide.with(this).load(str).into(target2);
    }
}